package com.tp.controller.system;

import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.tp.base.BaseController;
import com.tp.model.sys.SysLog;
import com.tp.model.sys.SysUser;
import com.tp.service.LogService;

/**
 * 操作日志
 * 
 * @author TP
 *
 */
@ControllerBind(controllerKey = "/system/log")
public class LogController extends BaseController {

	private static final String path = "/pages/system/log/log_";

	@Inject
	LogService service;

	public void index() {
		render(path + "list.html");
	}

	public void list() {
		SysLog model = getModelByAttr(SysLog.class);
		// 排序
		String orderBy = getBaseForm().getOrderBy();
		Page<SysLog> page = service.getPageByWhere(model, orderBy, getPaginator());
		renderCount(page.getTotalRow(), page.getList());
	}

	public void view() {
		SysLog model = new SysLog().dao().findFirst("select t.*,u.logn_acct as create_name from " + SysLog.tableName + " t left join " + SysUser.tableName
				+ " u on t.create_id=u.id where t.id=?", getParaToInt("id", 0));
		setAttr("model", model);
		render(path + "view.html");
	}

	@Before(Tx.class)
	public void delete() {
		String[] ids = getPara("ids").split(",");
		boolean flag = service.deleteObject(ids);
		if (flag) {
			renderOk("删除成功");
		} else {
			renderFail("删除失败");
		}
	}

	public void clearLog() {
		boolean flag = service.clearLog();
		if (flag) {
			renderOk("删除成功");
		} else {
			renderFail("删除失败");
		}
	}

}
